package com.hunan.api.catv.service.impl.tencent_cloud.ims;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.utils.GsonUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.Region;
import com.tencentcloudapi.ims.v20200713.ImsClient;
import com.tencentcloudapi.ims.v20200713.models.ImageModerationRequest;
import com.tencentcloudapi.ims.v20200713.models.ImageModerationResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ImsService {

    public static final Logger LOGGER = LoggerFactory.getLogger(ImsService.class);

    @Autowired
    private CacheBiz cacheBiz;

    public ImageModerationResponse imageModeration(String url) {
        ImageModerationResponse response = null;
        if (StringUtils.isNotEmpty(url)) {
            ImageModerationRequest request = new ImageModerationRequest();
            request.setFileUrl(url);
            try {
                response = getImsClient().ImageModeration(request);
            } catch (TencentCloudSDKException e) {
                LOGGER.error("腾讯图片审核出错：", e);
            }
        }
        return response;
    }

    private ImsClient getImsClient() {
        JsonObject imsConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_ims_conf), JsonObject.class);
        String secretId = imsConf.get(Constants.tencent_cloud_secret_id_name).getAsString();
        String secretKey = imsConf.get(Constants.tencent_cloud_secret_key_name).getAsString();
        Credential credential = new Credential(secretId, secretKey);
        ImsClient imsClient = new ImsClient(credential, Region.Guangzhou.getValue());
        return imsClient;
    }

}
